package model;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import util.MD5Code;

/**
 * A data access object (DAO) providing persistence and search support for Admin
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see model.Admin
 * @author MyEclipse Persistence Tools
 */

public class AdminDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(AdminDAO.class);
	// property constants
	public static final String USERNAME = "username";
	public static final String USERPASSWORD = "userpassword";

	protected void initDao() {
		// do nothing
	}

	/**
	 * 验证登陆
	 * @param teacherusername
	 * @param teacherpassword
	 * @return
	 */
	public String loginValidate(String username,String userpassword)
	{
		MD5Code m = new MD5Code();
		String hql="FROM Admin AS a WHERE a.username=:username";
		Query q=super.getSession().createQuery(hql);
		q.setString("username", username);
		if(q.list().size()==0)
			return "UserNotExist";
		
		String hql1="FROM Admin AS a WHERE a.username=:username and a.userpassword=:userpassword";
		Query q1=super.getSession().createQuery(hql1);
		q1.setString("username", username);
		q1.setString("userpassword", m.getMD5ofStr(userpassword));
		if(q1.list().size()==0)
			return "PasswordError";
		Admin a = new Admin();
		a =(Admin) q1.list().get(0);
		return a.getId().toString();
	}
	
	/**
	 * 修改密码
	 *
	 */
	public void editPassword(String username,String userpasswordnew)
	{
		MD5Code m = new MD5Code();
		String hql="FROM Admin AS a WHERE a.username=:username";
		Query q=super.getSession().createQuery(hql);
		q.setString("username", username);
		Admin a = new Admin();
		a =(Admin) q.list().get(0);
		int id=a.getId();
		System.out.println("该用户的id为："+id);
		System.out.println("该用户的id为："+userpasswordnew);
		String hql1="UPDATE Admin AS a SET a.userpassword=:userpasswordnew WHERE a.id=:id";
		Query q1=super.getSession().createQuery(hql1);
		q1.setInteger("id", id);
		q1.setString("userpasswordnew", m.getMD5ofStr(userpasswordnew));
		q1.executeUpdate();
	}
	
	public void save(Admin transientInstance) {
		log.debug("saving Admin instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Admin persistentInstance) {
		log.debug("deleting Admin instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Admin findById(java.lang.Integer id) {
		log.debug("getting Admin instance with id: " + id);
		try {
			Admin instance = (Admin) getHibernateTemplate().get("model.Admin",
					id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Admin instance) {
		log.debug("finding Admin instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Admin instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Admin as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByUsername(Object username) {
		return findByProperty(USERNAME, username);
	}

	public List findByUserpassword(Object userpassword) {
		return findByProperty(USERPASSWORD, userpassword);
	}

	public List findAll() {
		log.debug("finding all Admin instances");
		try {
			String queryString = "from Admin";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Admin merge(Admin detachedInstance) {
		log.debug("merging Admin instance");
		try {
			Admin result = (Admin) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Admin instance) {
		log.debug("attaching dirty Admin instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Admin instance) {
		log.debug("attaching clean Admin instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static AdminDAO getFromApplicationContext(ApplicationContext ctx) {
		return (AdminDAO) ctx.getBean("AdminDAO");
	}
}